import global_param
from normal import get_directed_file
from PIL import Image
import cv2
import numpy as np

if __name__ == "__main__":
    mode = 'cv'
    pic_path = global_param._global_dict['root_path']
    root_path = global_param._global_dict['root_path']
    pic_path = root_path + pic_path
    pic_type = ('.bmp', '.dib', '.png', '.jpg', '.jpeg', '.pbm', '.pgm', '.ppm', '.tif', '.tiff')

    pic_list = get_directed_file(pic_path, pic_type)

    # 约定缩放比例，高度调整为
    hh = 460
    for i, pic in enumerate(pic_list):

        pic = pic_path + pic
        if mode == 'pil':
            pic_ = Image.open(pic)
            w = pic_.size[0]
            h = pic_.size[1]
        elif mode == 'cv':
            # cv识别不了中文https://blog.csdn.net/weixin_42111770/article/details/87003617
            pic_ = cv2.imread(pic)
            # pic_ = cv2.imdecode(np.fromfile(pic, dtype=np.uint8), -1)
            h, w = pic_.shape[:2]
        print(pic_list[i])
        scale = hh / h
        resized_pic_path = pic_path + pic_list[i][:-4] + '_' + pic_list[i][-4:]
        if mode == 'pil':
            # pil方法
            pic_.thumbnail((int(w * scale), hh))
            pic_.save(resized_pic_path, quality=95)
            print('changed size', pic_.size)
        elif mode == 'cv':
            # cv方法
            resized_pic = cv2.resize(pic_, (int(w * scale),hh))
            cv2.imwrite(resized_pic_path, resized_pic)
            print('changed size', resized_pic.shape[:2])
